<!DOCTYPE html>
<!--
Copyright 2013 Austin France

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<style type="text/css">
th { text-align: left; }
</style>
<script src="json.js"></script>
<script src="DEBUG.js"></script>
<script src="../lib/xhr-pt.js"></script>
<script src="../lib/xhr-pt.js"></script>
<table border="1" cellpadding="5" width="100%">
<thead><tr><th width="70px">Request ID</th><th>Log</th></tr></thead>
<tbody id="results"></tbody>
</table>
<script>

function response(response) {
	switch(response.eventType) {
	case this.EVENT_TYPE.LOADING:
		var xhr = response.getXhr();
		if (xhr.readyState > 2) {
			try {
				DEBUG(this.getId(), " downloading ... " + (parseInt((xhr.responseText.length/1024.0)*100)/100.0) + " KiB");
			} catch(e) {
				// Old IE don't like accessing responseText when readyState < 4
				DEBUG(this.getId(), " downloading ... ");
			}
		}
		break;
	case this.EVENT_TYPE.TIMEOUT:
		DEBUG(this.getId(), "Timed Out");
		console.error('timed out');
		console.dir(response); 
		break;
	case this.EVENT_TYPE.ERROR:
		var e = response.error, m = "[code=" + e.code + "] " + e.message + ": " + e.stack;
		console.error(m);
		DEBUG(this.getId(), m);
		console.dir(response); 
		break;
	case this.EVENT_TYPE.LOAD:
		console.dir(response);
		var xhr = response.getXhr();
		DEBUG(this.getId(), "DONE " + response.XHR.method + " " + xhr.status + " " + xhr.statusText + " > " 
					+ (response.XHR.method != "GET" ? response.getXhr().responseText : ""));
		break;
	}
}

// Full blow GET request, with embedded query parameters, timeout, custom header 
// and handler for all the different events (well most of them).

DEBUG(0,"GET slowdown.php?s=102400&d=0.1");
XHR("slowdown.php")
	.addId()
    .debug(true)
	.query("s",102400)
	.query("d", 0.1)
	.timeout(10000)
	.header("X-Test-Header", "header value")
	.addEventListener("timeout", response)
	.addEventListener("loading", response)
	.addEventListener("error",   response)
	.addEventListener("load",   response)
	.start();

// Same request, but query string is already embedded in the URI
DEBUG(1,"GET slowdown.php?s=102400&d=0.1");
XHR("slowdown.php?s=102400&d=0.1")
	.addId()
    .debug(true)
	.timeout(10000)
	.on("timeout", response)
	.on("loading", response)
	.on("error",   response)
	.on("load",   response)
	.start();

// POST request, sending form-encoded data

DEBUG(2,"POST slowdown.php");
XHR("slowdown.php")
	.addId()
    .debug(true)
	.post("name=value&name2=value2", "application/x-www-form-encoded")
	.timeout(1000)
	.ontimeout(response)
	.onloading(response)
	.onerror(response)
	.onload(response)
	.start();

// PUT Request sending JSON data

DEBUG(3,"PUT slowdown.php");
XHR("slowdown.php")
	.addId()
    .debug(true)
	.timeout(1000)
	.put({ name: "value" })
	.on("timeout", response)
	.on("loading", response)
	.on("error",   response)
	.on("load",   response)
	.start();

// HEAD request

DEBUG(4,"HEAD slowdown.php");
XHR("slowdown.php")
	.addId()
    .debug(true)
	.head()
	.timeout(1000)
	.on("timeout", response)
	.on("loading", response)
	.on("error",   response)
	.on("load",   response)
	.start();

// OPTIONS request

DEBUG(5,"OPTIONS slowdown.php");
XHR("slowdown.php")
	.addId()
    .debug(true)
	.options()
	.timeout(1000)
	.on("timeout", response)
	.on("loading", response)
	.on("error",   response)
	.on("load",   response)
	.start();

// Test for Origin is not Allowed handling

DEBUG(6,"Origin is not Allowed");
try { 
	XHR("https://xhr-progressive-timeouts.googlecode.com/hg/trunk/lib/xhr-pt.js")
		.addId()
		.debug(true)
		.timeout(1000)
		.on("timeout", response)
		.on("loading", response)
		.on("error",   function(response) { 
			var e = response.error, m = "[code=" + e.code + "] " + e.message + ": " + e.stack;
			console.error(m);
			if (e.code == 0) {
				DEBUG(6, "Origin is Not Allowed: Successfully failed with e.code == 0");
			} else {
				DEBUG(6, "Unexpected Result: " + m);
			}
		})
		.on("load",   response)
		.start();
} catch(e) {
	DEBUG(6, e.message);
}

// Simple GET request

DEBUG(7,"Simple");
XHR("slowdown.php", function(response) { 
	DEBUG(7, response.getXhr().status + " OK " + response.XHR.getFinalUri()); 
});

// Simple POST request with credentials
DEBUG(8,"Simple POST");
XHR("slowdown.php", [ "POST", "somedata", "adf", "password" ], function(response) { 
	DEBUG(8, response.getXhr().status + " OK " + response.XHR.getFinalUri()); 
});


// Simple Request modified by the call chain
DEBUG(9,"Simple GET modified");
XHR("slowdown.php", function(response) { 
	DEBUG(9, response.getXhr().status + " OK " + response.XHR.getFinalUri()); 
}).addId().query("name", "value");


// Simple Request modified by the call chain
DEBUG(10,"Example using done");
XHR("slowdown.php").done(	function(response) { 
								DEBUG(10, response.getXhr().status + " OK " + response.XHR.getFinalUri()); 
							},
							function(response) {
								DEBUG(10, " ERROR " + response.error.message); 
							},
							function(response) {
								DEBUG(10, " TIMEOUT");
							}
						);

// Recursive XHR request: Seen a problem where cross-domain security was triggered in chrome even though we
// are not cross-domain XHRing.  It seemed to be triggered by recursive XHR requests, though I don't seem
// to be able to re-produce it here.
DEBUG(11,"Recursive XHR (chrome-bug) security test...");
XHR("slowdown.php")
	.debug(true)
	.addId()
	.query({a:"1"})
	.timeout(1000)
	.onload(function(response) {
		DEBUG(11, "Level 1: " + response.getXhr().status + " " + response.XHR.getFinalUri() + " should get to level 3");
		XHR("slowdown.php").query({b:"2"}).debug(true).timeout(1000).onload(
			function(response) {
				DEBUG(11, "Level 2: " + response.getXhr().status + " " + response.XHR.getFinalUri() + " should get to level 3");
				XHR("slowdown.php").query({c:"3"}).debug(true).timeout(1000).onload(
					function(response) {
						DEBUG(11, "Level 3: " + response.getXhr().status + " " + response.XHR.getFinalUri() 
							+ " successfully reached level 3");
					}
				);
			}
		);
	});

// Test for done() third argument (timeout handler)
// https://code.google.com/p/xhr-progressive-timeouts/issues/detail?id=7
DEBUG(12,"Test for done() with timeout handler (Issue 7)");
XHR("slowdown.php")
	.debug(true)
	.addId()
	.query({ s: 10240, d: 1 })		// that will take 10+ seconds
	.timeout(1000)					// we will timeout in 1
	.done(function(response) { 
			DEBUG(12, response.getXhr().status + " OK " + response.XHR.getFinalUri() + " (TEST FAILED)"); 
		}, function(response) {
			DEBUG(12, " ERROR " + response.error.message + " (TEST FAILED)"); 
		}, function(response) {
			DEBUG(12, " done() timeout handler called (TEST SUCCESSFUL)");
		});
</script>
